export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

import {formatMessage} from './intl';
export const renderBoolean = value => {
    if(typeof value !== 'boolean') return value;
    else if(value)
        return formatMessage({
            id: 'utils.yes',
            defaultMessage: '是'
        });
    return formatMessage({
        id: 'utils.no',
        defaultMessage: '否'
    });
};

export const isEmptyOrNull = value =>
    value === undefined || value === null || value === '';

/* eslint-disable prefer-const, no-bitwise, no-mixed-operators */
export const uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    let r = Math.random() * 16 | 0,
        v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
});

/**
 * [getPathById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} name   [需要获取的name]
 * @return {[Array]}      [path]
 */
export const getPathById = (data, name) => {
    for(let i = 0; i < data.length; i++) {
        const organization = data[i];
        /*eslint-disable  eqeqeq*/
        if(organization.name == name)
            return [i];
        if(organization.materials && organization.materials.length > 0) {
            const path = getPathById(organization.materials, name);
            if(path)
                return [i, 'materials'].concat(path);
        }
    }
};

import difference from 'lodash/difference';
/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        // 点击排序
        if(sorter.order && sorter.field) {
            option.sortField = sorter.field;
            if(sorter.order === 'descend')
                option.isDesc = true;
            else
                option.isDesc = false;
        } else { // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};

/**
 * 计算table的 selectedRows
 * @param {*} preKeys 上一个操作 返回的 selectedKeys
 * @param {*} preRows 上一个操作 返回的 selectedRows
 * @param {*} currentKeys 本次操作 返回的 selectedKeys
 * @param {*} currentRows 本次操作 返回的 selectedRows
 * @param {*} keyIndex eg: id
 */
export const computeSelectedRows = (preKeys, preRows, currentKeys, currentRows, keyIndex = 'id') => {
    if(currentKeys.length === 0)
        return [];
    // 勾选
    if(currentKeys.length > preKeys.length) {
        const newKeys = difference(currentKeys, preKeys);
        const newRows = [];
        newKeys.forEach(key => {
            const row = currentRows.find(r => r[keyIndex] === key);
            if(row)
                newRows.push(row);
        });
        return [...preRows, ...newRows];
    } else if(currentKeys.length < preKeys.length) { // 取消勾选
        const deleteKeys = difference(preKeys, currentKeys);
        const newRows = preRows.filter(d => !deleteKeys.includes(d[keyIndex]));
        return newRows;
    }
    return preRows;
};

